package thread;

import java.util.Random;

//取钱人
public class Taker extends  Thread {
    Accout ac;
    public Taker(String name,Accout ac){
        super(name);
        this.ac=ac;
    }

    @Override
    public void run() {
        Random r=new Random();
        while(ac.getMoney()>0){
            int m=r.nextInt(300);
            System.out.println("现在"+super.getName()+"取钱 "+m+"元,当前余额是"+ac.getMoney());
            synchronized (ac) {
                if (ac.getMoney() >= m) {
                    try {
                        sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    ac.withDraw(m);
                    System.out.println(super.getName() + "取钱 " + m + "元成功,当前余额是" + ac.getMoney());
                } else {
                    System.out.println(super.getName() + "取钱 " + m + "元失败,余额不足,当前余额是" + ac.getMoney());
                }
            }

        }
        System.out.println("当前余额是"+ac.getMoney());
    }
}
